local sk_le__xiaoji = fk.CreateSkill {

  name = "sk_le__xiaoji",

  tags = {  },

}



sk_le__xiaoji:addEffect(fk.AfterCardsMove, {
  name = "sk_le__xiaoji",
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk_le__xiaoji.name) then
      for _, move in ipairs(data) do
        if move.from and move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerEquip then
              return true
            end
          end
        end
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    for _, move in ipairs(data) do
      if move.from and move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip then
            if not player:hasSkill(sk_le__xiaoji.name) or self.cancel_cost then return end
            event:setCostData(self, info.cardId)
            self:doCost(event, target, player, data)
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(player, sk_le__xiaoji.name) then
      return true
    end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(1, sk_le__xiaoji.name)
    if player.dead then return end
    local card = Fk:getCardById(event:getCostData(self))
    if card.sub_type == Card.SubtypeWeapon then
      if #room.alive_players == 1 then return end
      local to = room:askForChoosePlayers(player, table.map(room:getOtherPlayers(player), Util.IdMapper), 1, 1,
        "#sk_le__xiaoji-damage", sk_le__xiaoji.name, true)
      if #to > 0 then
        room:damage({
          from = player,
          to = room:getPlayerById(to[1]),
          damage = 1,
          skillName = sk_le__xiaoji.name,
        })
      end
    elseif card.sub_type == Card.SubtypeArmor or card.sub_type == Card.SubtypeTreasure then
      player:drawCards(2, sk_le__xiaoji.name)
    elseif card.sub_type == Card.SubtypeOffensiveRide or card.sub_type == Card.SubtypeDefensiveRide then
      for i = 1, 2, 1 do
        local targets = table.map(table.filter(room:getOtherPlayers(player), function(p)
          return not p:isNude() end), Util.IdMapper)
        if player.dead or #targets == 0 then return end
        local to = room:askForChoosePlayers(player, targets, 1, 1, "#sk_le__xiaoji-discard", sk_le__xiaoji.name, true)
        if #to > 0 then
          to = room:getPlayerById(to[1])
          local id = room:askForCardChosen(player, to, "he", sk_le__xiaoji.name)
          room:throwCard(id, sk_le__xiaoji.name, to, player)
        else
          return
        end
      end
    end
  end,
})

return sk_le__xiaoji